package com.dai.controller;

import com.dai.bean.OpResult;
import com.dai.bean.Task;
import com.dai.bean.User;
import com.dai.bean.ZaoWanData;
import com.dai.bean.tasks.TaskHeShui;
import com.dai.bean.tasks.TaskZaoWan;
import com.dai.dao.TaskDAO;
import com.dai.dao.UserDAO;
import com.dai.tools.Awards;
import com.dai.tools.RetMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TaskController {

    @Autowired
    private TaskDAO taskDAO;

    @Autowired
    private UserDAO userDAO;

    @GetMapping("/task/zaowan")
    public RetMsg<TaskZaoWan> getTaskZaoWan(
            @RequestParam(name = "user_id") int user_id
    ) {
        TaskZaoWan zw = taskDAO.getTaskZaoWan(user_id);
        if (zw == null) {
            int errCode = RetMsg.Err_Null_Data;
            return RetMsg.makeErrObj(errCode, RetMsg.getMsg(errCode));
        } else {
            return RetMsg.makeSuccessObj(zw);
        }
    }

    @GetMapping("/task/heshui")
    public RetMsg<TaskHeShui> getTaskHeShui(
            @RequestParam(name = "user_id") int user_id
    ) {
        TaskHeShui hs = taskDAO.getTaskHeShui(user_id);
        if (hs == null) {
            int errCode = RetMsg.Err_Null_Data;
            return RetMsg.makeErrObj(errCode, RetMsg.getMsg(errCode));
        } else {
            return RetMsg.makeSuccessObj(hs);
        }
    }

    // 早晚打卡
    @GetMapping("/task/zaowan_daka")
    public RetMsg<OpResult> zaowanDaKa(
            @RequestParam(name = "user_id") int user_id,
            @RequestParam(name = "state_value") int stateValue,
            @RequestParam(name = "time_value") int timeValue
    ) {
        Task task = taskDAO.getTask(user_id);
        TaskZaoWan zw = taskDAO.getTaskZaoWan(task);

        // 可以进一步检查
        if (timeValue == ZaoWanData.Time.ZAO.getValue()) {
            if (zw.zaoExecuted > 0) {
                throw new RuntimeException("早上已经打卡,可能出现错误.....");
            }
            zw.zaoExecuted = 1;
        } else if (timeValue == ZaoWanData.Time.WAN.getValue()) {
            if (zw.wanExecuted > 0) {
                throw new RuntimeException("晚上已经打卡,可能出现错误.....");
            }
            zw.wanExecuted = 1;
        }

        // 更新并存储
        task.updateUserData(zw);
        taskDAO.updateTask(task);

        // 判断是正常打卡还是补卡
        // TODO: 区分是否是双倍
        // TODO: 是否需要把有视频标识传递过来
        // TODO: 设计一个合适的返回消息格式
        OpResult result = new OpResult(Awards.CoinZaoWanDaKa);

        // 还要给用户增加金币
        User user = userDAO.getUserByUserId(user_id);
        user.coin += Awards.CoinZaoWanDaKa;
        userDAO.updateUserCoin(user);

//        if (stateValue == ZaoWanData.State.NormalDaKa.getValue()) {
//            award = Awards.CoinZaoWanDaKa;
//        } else if (stateValue == ZaoWanData.State.BuKa.getValue()) {
//            award = Awards.CoinZaoWanDaKa;
//        }
        // 返回数据
        return RetMsg.makeSuccessObj(result);
    }


    // 喝水打卡
    @GetMapping("/task/heshui_daka")
    public RetMsg<OpResult> heshuiDaKa(
            @RequestParam(name = "user_id") int user_id,
            @RequestParam(name = "heshui_id") int heshui_id
    ){
        Task task = taskDAO.getTask(user_id);
        TaskHeShui hs = taskDAO.getTaskHeShui(task);
        hs.updateValue(heshui_id,1);
        task.updateUserData(hs);
        taskDAO.updateTask(task);
        OpResult result = new OpResult(Awards.CoinHeShuiDaKa);

        // 还要给用户增加金币
        User user = userDAO.getUserByUserId(user_id);
        user.coin += Awards.CoinHeShuiDaKa;
        userDAO.updateUserCoin(user);

        return RetMsg.makeSuccessObj(result);
    }
}
